<!DOCTYPE html>
<html lang="zh_cn">
<head>
          <title>来玩魔王的咚</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta charset="utf-8" />
        <!-- twitter card metadata -->
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/images/mowang.png">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="shell 监测进程">
<meta name="twitter:description" content="<p>ps命令查看进程：Unix风格ps -ef，BSD风格ps aux，GNU长参数；top命令实时监测进程；结束进程：Linux进程信号，klll命令，killall命令</p>">
        <!-- OG Tags -->
<meta property="og:url" content="/shell-process-monitor.html"/>
<meta property="og:title" content="来玩魔王的咚 | shell 监测进程" />
<meta property="og:description" content="<p>ps命令查看进程：Unix风格ps -ef，BSD风格ps aux，GNU长参数；top命令实时监测进程；结束进程：Linux进程信号，klll命令，killall命令</p>" />
        <!-- favicon -->
        <link rel="icon" type="image/png" href="/images/mowang.png">
        <!-- moment.js for date formatting -->
        <script src="/theme/js/moment.js"></script>
        <!-- css -->
        <link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
        <!-- 左边的menu，如果页面高度不够，就跟着滚动，否则文章分类显示不全 -->
        <link rel="stylesheet" type="text/css" href="/theme/css/mycss/menu.css" />
		<script>
			
                /*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
    
    (function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
                
                grunticon(["/theme/css/icons.data.svg.css", "/theme/css/icons.data.png.css", "/theme/css/icons.fallback.css"]);
            </script>
        <noscript><link href="/theme/css/icons.fallback.css" rel="stylesheet"></noscript>
        <!-- menu toggle javascript -->
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", initMenu);
            
            function initMenu(){
                var menu = document.getElementById("menu");
                var menulink = document.getElementById("menu-link");
                menulink.addEventListener("click", function toggleMenu(){
                        window.event.preventDefault();
                        menulink.classList.toggle('active');
                        menu.classList.toggle('active');              
                    });
            };
        </script>
        <!-- 不蒜子 -->
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <meta name="description" content="<p>ps命令查看进程：Unix风格ps -ef，BSD风格ps aux，GNU长参数；top命令实时监测进程；结束进程：Linux进程信号，klll命令，killall命令</p>" />

    <meta name="tags" content="shell" />
  <!-- 替换部分base的样式，看文章时，要再宽一点，右边有很多空间可以撑开 -->
  <link rel="stylesheet" type="text/css" href="/theme/css/mycss/article.css" />

</head>
<body>
    <div role="banner" id="masthead">
        <header>
            <a href="/"><img src="/images/mowang.png" alt="McManus Logo"></a>
                <h1>来玩魔王的咚@骑士救兵</h1>
            <a href="#menu" id="menu-link">more stuff</a>
            <nav id="menu">
                <ul>
                        <li><a href="/tags">tags</a></li>
                            <li><a href="/category/cloud.html">Cloud</a></li>
                            <li><a href="/category/docker.html">Docker</a></li>
                            <li><a href="/category/go.html">Go</a></li>
                            <li class="active"><a href="/category/linux.html">Linux</a></li>
                            <li><a href="/category/python.html">Python</a></li>
                            <li><a href="/category/xue-xi-bi-ji.html">学习笔记</a></li>
                            <li><a href="/category/yun-wei-zi-dong-hua.html">运维自动化</a></li>
                </ul>
            </nav>
        </header>
    </div>
        <div class="page" role="main">
  <div class="article" role="article">
    <article>
        <footer>
            <a name="top"></a>
            <p>
              <time datetime=" 2020-09-23 10:00:00+08:00">
                <script>document.write(moment('2020-09-23 10:00:00+08:00').format('LL'));</script>
              </time>
              ~
              <time datetime=" 2020-09-23 10:00:00+08:00">
                <script>document.write(moment('2020-09-23 10:00:00+08:00').format('LL'));</script>
              </time>
            </p>
        </footer>
        <header>
          <h2>
            shell 监测进程
          </h2>
        </header>
      <div class="content">
         <div class="toc">
<ul>
<li><a href="#cha-kan-jin-cheng-ps">查看进程(ps)</a><ul>
<li><a href="#unix-feng-ge-de-can-shu">Unix风格的参数</a></li>
<li><a href="#bsd-feng-ge-de-can-shu">BSD风格的参数</a></li>
<li><a href="#gnu-zhang-can-shu">GNU长参数</a></li>
</ul>
</li>
<li><a href="#shi-shi-jian-ce-jin-cheng-top">实时监测进程(top)</a></li>
<li><a href="#jie-shu-jin-cheng-kill">结束进程(kill)</a><ul>
<li><a href="#linux-jin-cheng-xin-hao">Linux进程信号</a></li>
<li><a href="#fa-song-xin-hao-jie-shu-jin-cheng">发送信号结束进程</a></li>
</ul>
</li>
</ul>
</div>
<h3 id="cha-kan-jin-cheng-ps"><a class="toclink" href="#cha-kan-jin-cheng-ps">查看进程(ps)</a></h3>
<p>当程序运行在系统上时，称之为进程（process）。要监测这些进程，就需要熟悉ps命令的用法。  </p>
<p>默认情况下，ps命令只会显示运行在当前控制台下的属于当前用户的进程：</p>
<div class="highlight"><pre><span></span><code>$ ps
  PID TTY          TIME CMD
 <span class="m">4763</span> pts/0    <span class="m">00</span>:00:00 bash
 <span class="m">4782</span> pts/0    <span class="m">00</span>:00:00 ps
$ 
</code></pre></div>

<p>在这里，当前用户只运行了bash shell（注意，shell也只是运行在系统上的另一个进程而已），以及ps命令本身。  </p>
<p>Linux系统中使用的GNU ps命令支持3种不同类型的命令行参数：</p>
<ul>
<li>Unix风格的参数，前面加单破折线</li>
<li>BSD风格的参数，前面不加破折线</li>
<li>GNU风格的长参数，前面加双破折线</li>
</ul>
<h4 id="unix-feng-ge-de-can-shu"><a class="toclink" href="#unix-feng-ge-de-can-shu">Unix风格的参数</a></h4>
<p>Unix风格的参数是从贝尔实验室开发的AT&amp;T Unix系统上原有的ps命令继承下来的。<br>
<strong>Unix风格的ps命令参数：</strong>  </p>
<table>
<thead>
<tr>
<th align="left">参数</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">-A</td>
<td align="left">显示所有进程</td>
</tr>
<tr>
<td align="left">-N</td>
<td align="left">显示与指示参数不符的所有进程</td>
</tr>
<tr>
<td align="left">-a</td>
<td align="left">显示除控制进程（session leader）和无终端进程外的所有进程</td>
</tr>
<tr>
<td align="left">-d</td>
<td align="left">显示除控制进程外的所有进程</td>
</tr>
<tr>
<td align="left">-e</td>
<td align="left">显示所有进程。<em>应该和-A是一样的</em></td>
</tr>
<tr>
<td align="left">-C cmdlist</td>
<td align="left">显示包含在cmdlist列表中的进程</td>
</tr>
<tr>
<td align="left">-G grplist</td>
<td align="left">显示组ID在grplist列表中的进程</td>
</tr>
<tr>
<td align="left">-U userlist</td>
<td align="left">显示属主的用户ID在userlist列表中的进程</td>
</tr>
<tr>
<td align="left">-g grplist</td>
<td align="left">显示会话或组ID在grplist列表中的进程</td>
</tr>
<tr>
<td align="left">-p pidlist</td>
<td align="left">显示PID在pidlist列表中的进程</td>
</tr>
<tr>
<td align="left">-s sesslist</td>
<td align="left">显示会话ID在sesslist列表中的进程</td>
</tr>
<tr>
<td align="left">-t ttylist</td>
<td align="left">显示终端ID在ttylist列表中的进程</td>
</tr>
<tr>
<td align="left">-u userlist</td>
<td align="left">显示有效用户ID在userlist列表中的进程</td>
</tr>
<tr>
<td align="left">-F</td>
<td align="left">显示更多额外输出（相对-f参数而言）</td>
</tr>
<tr>
<td align="left">-O format</td>
<td align="left">显示默认的输出列以及format列表指定的特定列</td>
</tr>
<tr>
<td align="left">-M</td>
<td align="left">显示进程的安全信息</td>
</tr>
<tr>
<td align="left">-c</td>
<td align="left">显示进程的额外调度器信息</td>
</tr>
<tr>
<td align="left">-f</td>
<td align="left">显示完整格式的输出</td>
</tr>
<tr>
<td align="left">-j</td>
<td align="left">显示任务信息</td>
</tr>
<tr>
<td align="left">-l</td>
<td align="left">显示长列表</td>
</tr>
<tr>
<td align="left">-o format</td>
<td align="left">仅显示由format指定的列</td>
</tr>
<tr>
<td align="left">-y</td>
<td align="left">不要显示进程标记（process flag，表名进程状态的标记）</td>
</tr>
<tr>
<td align="left">-Z</td>
<td align="left">显示安全标签（SELinux的security context）信息</td>
</tr>
<tr>
<td align="left">-H</td>
<td align="left">用层级格式来显示进程（树状，用来显示父进程）</td>
</tr>
<tr>
<td align="left">-n namelist</td>
<td align="left">定义了WCHAN列显示的值</td>
</tr>
<tr>
<td align="left">-w</td>
<td align="left">采用宽输出模式，不限宽度显示</td>
</tr>
<tr>
<td align="left">-L</td>
<td align="left">显示进程中的线程</td>
</tr>
<tr>
<td align="left">-V</td>
<td align="left">显示ps命令的版本号</td>
</tr>
</tbody>
</table>
<p>命令参数列出了很多，但是不一定全。使用ps命令的关键不在于记住多少可用的参数。而是只要记住最有用的一些参数组合就好了。  </p>
<h5 id="ps-ef"><a class="toclink" href="#ps-ef">ps -ef</a></h5>
<p>查看系统上运行的所有进程：</p>
<div class="highlight"><pre><span></span><code>$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         <span class="m">1</span>     <span class="m">0</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize <span class="m">22</span>
root         <span class="m">2</span>     <span class="m">0</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 <span class="o">[</span>kthreadd<span class="o">]</span>
root         <span class="m">3</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 <span class="o">[</span>ksoftirqd/0<span class="o">]</span>
root         <span class="m">5</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 <span class="o">[</span>kworker/0:0H<span class="o">]</span>
root         <span class="m">6</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 <span class="o">[</span>kworker/u2:0<span class="o">]</span>
root         <span class="m">7</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 <span class="o">[</span>migration/0<span class="o">]</span>
root         <span class="m">8</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 <span class="o">[</span>rcu_bh<span class="o">]</span>
root         <span class="m">9</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 <span class="o">[</span>rcu_sched<span class="o">]</span>
root        <span class="m">10</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 <span class="o">[</span>lru-add-drain<span class="o">]</span>

......

root      <span class="m">4250</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 /usr/sbin/hypervkvpd -n
root      <span class="m">4251</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 /usr/sbin/sshd -D
root      <span class="m">4253</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:01 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      <span class="m">4254</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 /usr/sbin/rsyslogd -n
root      <span class="m">4515</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">18</span>:23 ?        <span class="m">00</span>:00:00 /usr/libexec/postfix/master -w
postfix   <span class="m">4532</span>  <span class="m">4515</span>  <span class="m">0</span> <span class="m">18</span>:24 ?        <span class="m">00</span>:00:00 qmgr -l -t unix -u
root      <span class="m">4569</span>  <span class="m">4251</span>  <span class="m">0</span> <span class="m">18</span>:24 ?        <span class="m">00</span>:00:00 sshd: root@pts/0
root      <span class="m">4763</span>  <span class="m">4569</span>  <span class="m">0</span> <span class="m">18</span>:24 pts/0    <span class="m">00</span>:00:00 -bash
postfix   <span class="m">4931</span>  <span class="m">4515</span>  <span class="m">0</span> <span class="m">20</span>:04 ?        <span class="m">00</span>:00:00 pickup -l -t unix -u
root      <span class="m">4938</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">20</span>:31 ?        <span class="m">00</span>:00:00 <span class="o">[</span>kworker/0:3<span class="o">]</span>
root      <span class="m">4944</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">20</span>:46 ?        <span class="m">00</span>:00:00 <span class="o">[</span>kworker/0:2<span class="o">]</span>
root      <span class="m">4947</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">20</span>:51 ?        <span class="m">00</span>:00:00 <span class="o">[</span>kworker/0:0<span class="o">]</span>
root      <span class="m">4949</span>  <span class="m">4763</span>  <span class="m">0</span> <span class="m">20</span>:53 pts/0    <span class="m">00</span>:00:00 ps -ef
$ 
</code></pre></div>

<p>每列的信息说明：</p>
<ul>
<li>UID：启动这些进程的用户</li>
<li>PID：进程的进程ID</li>
<li>PPID：父进程的进程号（如果该进程是由另一个进程启动的）</li>
<li>C：进程生命周期中的CPU利用率</li>
<li>STIME：进程启动时的系统时间</li>
<li>TTY：进程启动时的终端设备</li>
<li>TIME：运行进程需要的累积CPU时间</li>
<li>CMD：启动的程序名称</li>
</ul>
<h5 id="ps-el"><a class="toclink" href="#ps-el">ps -el</a></h5>
<p><code>ps -ef</code> 最常用，而-l参数可以显示其他额外的信息，也可以<code>ps -efl</code>把两部分的信息都显示出来。<br>
使用-l参数，会产生一个长格式输出：</p>
<div class="highlight"><pre><span></span><code>$ ps -el
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
<span class="m">4</span> S     <span class="m">0</span>     <span class="m">1</span>     <span class="m">0</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">32005</span> ep_pol ?        <span class="m">00</span>:00:01 systemd
<span class="m">1</span> S     <span class="m">0</span>     <span class="m">2</span>     <span class="m">0</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> kthrea ?        <span class="m">00</span>:00:00 kthreadd
<span class="m">1</span> S     <span class="m">0</span>     <span class="m">3</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> smpboo ?        <span class="m">00</span>:00:00 ksoftirqd/0
<span class="m">1</span> S     <span class="m">0</span>     <span class="m">5</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">60</span> -20 -     <span class="m">0</span> worker ?        <span class="m">00</span>:00:00 kworker/0:0H
<span class="m">1</span> S     <span class="m">0</span>     <span class="m">6</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> worker ?        <span class="m">00</span>:00:00 kworker/u2:0
<span class="m">1</span> S     <span class="m">0</span>     <span class="m">7</span>     <span class="m">2</span>  <span class="m">0</span> -40   - -     <span class="m">0</span> smpboo ?        <span class="m">00</span>:00:00 migration/0
<span class="m">1</span> S     <span class="m">0</span>     <span class="m">8</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> rcu_gp ?        <span class="m">00</span>:00:00 rcu_bh
<span class="m">1</span> R     <span class="m">0</span>     <span class="m">9</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> -      ?        <span class="m">00</span>:00:00 rcu_sched
<span class="m">1</span> S     <span class="m">0</span>    <span class="m">10</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">60</span> -20 -     <span class="m">0</span> rescue ?        <span class="m">00</span>:00:00 lru-add-drain

......

<span class="m">4</span> S     <span class="m">0</span>  <span class="m">4250</span>     <span class="m">1</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">10025</span> poll_s ?        <span class="m">00</span>:00:00 hypervkvpd
<span class="m">4</span> S     <span class="m">0</span>  <span class="m">4251</span>     <span class="m">1</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">28215</span> poll_s ?        <span class="m">00</span>:00:00 sshd
<span class="m">4</span> S     <span class="m">0</span>  <span class="m">4253</span>     <span class="m">1</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">143481</span> poll_s ?       <span class="m">00</span>:00:02 tuned
<span class="m">4</span> S     <span class="m">0</span>  <span class="m">4254</span>     <span class="m">1</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">55687</span> poll_s ?        <span class="m">00</span>:00:00 rsyslogd
<span class="m">5</span> S     <span class="m">0</span>  <span class="m">4515</span>     <span class="m">1</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">22412</span> ep_pol ?        <span class="m">00</span>:00:00 master
<span class="m">4</span> S    <span class="m">89</span>  <span class="m">4532</span>  <span class="m">4515</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">22455</span> ep_pol ?        <span class="m">00</span>:00:00 qmgr
<span class="m">4</span> S     <span class="m">0</span>  <span class="m">4569</span>  <span class="m">4251</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">39183</span> poll_s ?        <span class="m">00</span>:00:00 sshd
<span class="m">4</span> S     <span class="m">0</span>  <span class="m">4763</span>  <span class="m">4569</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">28859</span> do_wai pts/0    <span class="m">00</span>:00:00 bash
<span class="m">4</span> S    <span class="m">89</span>  <span class="m">4931</span>  <span class="m">4515</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">22438</span> ep_pol ?        <span class="m">00</span>:00:00 pickup
<span class="m">1</span> S     <span class="m">0</span>  <span class="m">4972</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> worker ?        <span class="m">00</span>:00:00 kworker/0:0
<span class="m">1</span> S     <span class="m">0</span>  <span class="m">4973</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> worker ?        <span class="m">00</span>:00:00 kworker/0:3
<span class="m">1</span> S     <span class="m">0</span>  <span class="m">4974</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> worker ?        <span class="m">00</span>:00:00 kworker/0:1
<span class="m">1</span> R     <span class="m">0</span>  <span class="m">4977</span>     <span class="m">2</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> -     <span class="m">0</span> -      ?        <span class="m">00</span>:00:00 kworker/0:2
<span class="m">0</span> R     <span class="m">0</span>  <span class="m">4979</span>  <span class="m">4763</span>  <span class="m">0</span>  <span class="m">80</span>   <span class="m">0</span> - <span class="m">38309</span> -      pts/0    <span class="m">00</span>:00:00 ps
$ ^
</code></pre></div>

<p>每列的信息说明：</p>
<ul>
<li>F：内核分配给进程的系统标记</li>
<li>S：进程的状态</li>
<li>O代表正在运行</li>
<li>S代表正在休眠</li>
<li>R代表可运行，正等待运行</li>
<li>Z代表僵化，进程已结束但父进程已不存在</li>
<li>T代表停止</li>
<li>PRI：进程的优先级（越大表示优先级越低）</li>
<li>NI：谦让度值用来参与决定优先级</li>
<li>ADDR：进程的内存地址</li>
<li>SZ：假如进程被换出，所需交换空间的大致大小</li>
<li>WCHAN：进程休眠的内核函数的地址</li>
</ul>
<h4 id="bsd-feng-ge-de-can-shu"><a class="toclink" href="#bsd-feng-ge-de-can-shu">BSD风格的参数</a></h4>
<p>伯克利软件发行版（Berkeley software distribution, BSD）是加州大学伯克利分校开发的一个Unix版本。<br>
<strong>BSD风格的ps命令参数：</strong>  </p>
<table>
<thead>
<tr>
<th align="left">参数</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">T</td>
<td align="left">显示跟当前终端关联的所有进程</td>
</tr>
<tr>
<td align="left">a</td>
<td align="left">显示跟任意终端关联的所有进程</td>
</tr>
<tr>
<td align="left">g</td>
<td align="left">显示所有的进程，包括控制进程</td>
</tr>
<tr>
<td align="left">r</td>
<td align="left">仅显示运行中的进程</td>
</tr>
<tr>
<td align="left">x</td>
<td align="left">显示所有的进程，甚至包括未分配任何终端的进程</td>
</tr>
<tr>
<td align="left">U userlist</td>
<td align="left">显示归userlist列表中某用户ID所拥有的进程</td>
</tr>
<tr>
<td align="left">p pidlist</td>
<td align="left">显示PID在pidlist列表中的进程</td>
</tr>
<tr>
<td align="left">t ttylist</td>
<td align="left">显示所关联的终端在ttylist列表中的进程</td>
</tr>
<tr>
<td align="left">O format</td>
<td align="left">除了默认输出的列之外，还输出由format指定的列</td>
</tr>
<tr>
<td align="left">X</td>
<td align="left">按过去的Linux i386寄存器格式显示</td>
</tr>
<tr>
<td align="left">Z</td>
<td align="left">将安全信息添加到输出中。效果和-Z参数差不多</td>
</tr>
<tr>
<td align="left">j</td>
<td align="left">显示任务信息</td>
</tr>
<tr>
<td align="left">l</td>
<td align="left">采用长模式</td>
</tr>
<tr>
<td align="left">o format</td>
<td align="left">仅显示由format指定的列</td>
</tr>
<tr>
<td align="left">s</td>
<td align="left">采用信号格式显示</td>
</tr>
<tr>
<td align="left">u</td>
<td align="left">采用基于用户的格式显示</td>
</tr>
<tr>
<td align="left">v</td>
<td align="left">采用虚拟内存格式显示</td>
</tr>
<tr>
<td align="left">N namelist</td>
<td align="left">定义在WCHAN列中使用的值</td>
</tr>
<tr>
<td align="left">O order</td>
<td align="left">定义显示信息列的顺序</td>
</tr>
<tr>
<td align="left">S</td>
<td align="left">将数值信息从子进程加到父进程上，比如CPU和内存的使用情况</td>
</tr>
<tr>
<td align="left">c</td>
<td align="left">显示真实的命令名称（用以启动进程的程序名称）</td>
</tr>
<tr>
<td align="left">e</td>
<td align="left">显示命令使用的环境变量</td>
</tr>
<tr>
<td align="left">f</td>
<td align="left">用分层格式来显示进程、表明哪些进程启动了哪些进程</td>
</tr>
<tr>
<td align="left">h</td>
<td align="left">不显示头信息</td>
</tr>
<tr>
<td align="left">k sort</td>
<td align="left">指定用以将输出排序的列</td>
</tr>
<tr>
<td align="left">n</td>
<td align="left">和WCHAN信息一起显示出来，用数值来表示用户ID和组ID</td>
</tr>
<tr>
<td align="left">w</td>
<td align="left">为较宽屏幕显示宽输出</td>
</tr>
<tr>
<td align="left">H</td>
<td align="left">将线程按进程来显示</td>
</tr>
<tr>
<td align="left">m</td>
<td align="left">在进程后显示线程</td>
</tr>
<tr>
<td align="left">L</td>
<td align="left">列出所有格式指定符</td>
</tr>
<tr>
<td align="left">V</td>
<td align="left">显示ps命令的版本号</td>
</tr>
</tbody>
</table>
<p>Unix和BSD类型的参数有很多是重复的，大多数情况下，选择自己常用的格式的参数即可。  </p>
<h5 id="ps-aux"><a class="toclink" href="#ps-aux">ps aux</a></h5>
<p>这个是BSD风格的显示所有进程的命令，效果类似于<code>ps -ef</code>，主要是展示风格不同，输出的列也有一些区别。<br>
在使用BSD参数时，ps命令会自动改变输出以模仿BSD格式：</p>
<div class="highlight"><pre><span></span><code>$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         <span class="m">1</span>  <span class="m">0</span>.0  <span class="m">0</span>.3 <span class="m">128000</span>  <span class="m">6540</span> ?        Ss   <span class="m">09</span>:19   <span class="m">0</span>:01 /usr/lib/systemd/systemd --switched-root --system --deserialize <span class="m">22</span>
root         <span class="m">2</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S    <span class="m">09</span>:19   <span class="m">0</span>:00 <span class="o">[</span>kthreadd<span class="o">]</span>
root         <span class="m">3</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S    <span class="m">09</span>:19   <span class="m">0</span>:00 <span class="o">[</span>ksoftirqd/0<span class="o">]</span>
root         <span class="m">5</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S&lt;   <span class="m">09</span>:19   <span class="m">0</span>:00 <span class="o">[</span>kworker/0:0H<span class="o">]</span>
root         <span class="m">6</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S    <span class="m">09</span>:19   <span class="m">0</span>:00 <span class="o">[</span>kworker/u2:0<span class="o">]</span>
root         <span class="m">7</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S    <span class="m">09</span>:19   <span class="m">0</span>:00 <span class="o">[</span>migration/0<span class="o">]</span>
root         <span class="m">8</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S    <span class="m">09</span>:19   <span class="m">0</span>:00 <span class="o">[</span>rcu_bh<span class="o">]</span>
root         <span class="m">9</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        R    <span class="m">09</span>:19   <span class="m">0</span>:00 <span class="o">[</span>rcu_sched<span class="o">]</span>
root        <span class="m">10</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S&lt;   <span class="m">09</span>:19   <span class="m">0</span>:00 <span class="o">[</span>lru-add-drain<span class="o">]</span>

......

root      <span class="m">4256</span>  <span class="m">0</span>.0  <span class="m">0</span>.0  <span class="m">40100</span>  <span class="m">1540</span> ?        Ss   <span class="m">09</span>:19   <span class="m">0</span>:00 /usr/sbin/hypervkvpd -n
root      <span class="m">4258</span>  <span class="m">0</span>.0  <span class="m">0</span>.2 <span class="m">112860</span>  <span class="m">4296</span> ?        Ss   <span class="m">09</span>:19   <span class="m">0</span>:00 /usr/sbin/sshd -D
root      <span class="m">4259</span>  <span class="m">0</span>.0  <span class="m">0</span>.4 <span class="m">222748</span>  <span class="m">7800</span> ?        Ssl  <span class="m">09</span>:19   <span class="m">0</span>:00 /usr/sbin/rsyslogd -n
root      <span class="m">4261</span>  <span class="m">0</span>.0  <span class="m">0</span>.9 <span class="m">573924</span> <span class="m">17144</span> ?        Ssl  <span class="m">09</span>:19   <span class="m">0</span>:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      <span class="m">4503</span>  <span class="m">0</span>.0  <span class="m">0</span>.1  <span class="m">89648</span>  <span class="m">2100</span> ?        Ss   <span class="m">09</span>:19   <span class="m">0</span>:00 /usr/libexec/postfix/master -w
postfix   <span class="m">4516</span>  <span class="m">0</span>.0  <span class="m">0</span>.2  <span class="m">89752</span>  <span class="m">4068</span> ?        S    <span class="m">09</span>:19   <span class="m">0</span>:00 pickup -l -t unix -u
postfix   <span class="m">4517</span>  <span class="m">0</span>.0  <span class="m">0</span>.2  <span class="m">89820</span>  <span class="m">4096</span> ?        S    <span class="m">09</span>:19   <span class="m">0</span>:00 qmgr -l -t unix -u
root      <span class="m">4770</span>  <span class="m">0</span>.0  <span class="m">0</span>.3 <span class="m">156732</span>  <span class="m">5524</span> ?        Ss   <span class="m">09</span>:28   <span class="m">0</span>:00 sshd: root@pts/0
root      <span class="m">4774</span>  <span class="m">0</span>.0  <span class="m">0</span>.1 <span class="m">115436</span>  <span class="m">2068</span> pts/0    Ss   <span class="m">09</span>:28   <span class="m">0</span>:00 -bash
root      <span class="m">4829</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S    <span class="m">09</span>:35   <span class="m">0</span>:00 <span class="o">[</span>kworker/0:3<span class="o">]</span>
root      <span class="m">4887</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        S    <span class="m">09</span>:40   <span class="m">0</span>:00 <span class="o">[</span>kworker/0:1<span class="o">]</span>
root      <span class="m">4901</span>  <span class="m">0</span>.0  <span class="m">0</span>.0      <span class="m">0</span>     <span class="m">0</span> ?        R    <span class="m">09</span>:45   <span class="m">0</span>:00 <span class="o">[</span>kworker/0:0<span class="o">]</span>
root      <span class="m">4903</span>  <span class="m">0</span>.0  <span class="m">0</span>.1 <span class="m">155360</span>  <span class="m">1884</span> pts/0    R+   <span class="m">09</span>:48   <span class="m">0</span>:00 ps aux
$ 
</code></pre></div>

<p>每列的信息说明：</p>
<ul>
<li>VSZ：进程在内存中的大小，以千字节（KB）为单位</li>
<li>RSS：进程在未唤出时占用的物理内存</li>
<li>STAT：代表当前进程状态的双字符状态码。</li>
</ul>
<p>BSD风格表示的进程状态码更详细。双字符码能比Unix风格输出的单字符状态码更清楚地表示进程的当前状态。<br>
第一个字符采用了Unix分格S列相同的值，表明休眠（S）、运行（O）还是等待（R）。<br>
第二个值进一步说明进程的状态：</p>
<ul>
<li>&lt;：该进程运行在高优先级上</li>
<li>N：该进程运行在低优先级上</li>
<li>L：该进程有页面锁定在内存中</li>
<li>s：该进程是控制进程</li>
<li>l：该进程是多线程的</li>
<li>+：该进程运行在前台</li>
</ul>
<h4 id="gnu-zhang-can-shu"><a class="toclink" href="#gnu-zhang-can-shu">GNU长参数</a></h4>
<p>最后，GNU开发人员在最近改进过的ps命令中加入了另外一些参数。其中一些复制了现有的Unix或BSD类型的参数，而另一些则提供了新功能。<br>
<strong>GNU风格的ps命令参数：</strong>  </p>
<table>
<thead>
<tr>
<th align="left">参数</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">--deselect</td>
<td align="left">显示所有进程，除了命令行中列出的进程</td>
</tr>
<tr>
<td align="left">--Group grplist</td>
<td align="left">显示组ID在grplist列表中的进程</td>
</tr>
<tr>
<td align="left">--User userlist</td>
<td align="left">显示用户ID在userlist列表中的进程</td>
</tr>
<tr>
<td align="left">--group grplist</td>
<td align="left">显示有效组ID在grplist列表中的进程</td>
</tr>
<tr>
<td align="left">--pid pidlist</td>
<td align="left">显示PID在pidlist列表中的进程</td>
</tr>
<tr>
<td align="left">--ppid pidlist</td>
<td align="left">显示父PID在pidlist列表中的进程</td>
</tr>
<tr>
<td align="left">--sid sidlist</td>
<td align="left">显示会话ID在sidlist列表中的进程</td>
</tr>
<tr>
<td align="left">--tty ttylist</td>
<td align="left">显示终端设备号在ttylist列表中的进程</td>
</tr>
<tr>
<td align="left">--user userlist</td>
<td align="left">显示有效用户ID在userlist列表中的进程</td>
</tr>
<tr>
<td align="left">--format format</td>
<td align="left">仅显示由format指定的列</td>
</tr>
<tr>
<td align="left">--context</td>
<td align="left">显示额外的安全信息</td>
</tr>
<tr>
<td align="left">--cols n</td>
<td align="left">将屏幕宽度设置为n列</td>
</tr>
<tr>
<td align="left">--columns n</td>
<td align="left">将屏幕宽度设置为n列</td>
</tr>
<tr>
<td align="left">--cumulative</td>
<td align="left">包含已停止的子进程的信息</td>
</tr>
<tr>
<td align="left">--forest</td>
<td align="left">用层级结构显示出进程和父进程之间的关系</td>
</tr>
<tr>
<td align="left">--headers</td>
<td align="left">在每页输出是显示列的头</td>
</tr>
<tr>
<td align="left">--no-headers</td>
<td align="left">不显示列的头</td>
</tr>
<tr>
<td align="left">--lines n</td>
<td align="left">将屏幕高度设为n行</td>
</tr>
<tr>
<td align="left">--rows n</td>
<td align="left">将屏幕高度设为n排</td>
</tr>
<tr>
<td align="left">--sort order</td>
<td align="left">指定将输出按哪列排序</td>
</tr>
<tr>
<td align="left">--width n</td>
<td align="left">将屏幕宽度设为n列</td>
</tr>
<tr>
<td align="left">--help</td>
<td align="left">显示帮助信息</td>
</tr>
<tr>
<td align="left">--info</td>
<td align="left">显示调试信息</td>
</tr>
<tr>
<td align="left">--version</td>
<td align="left">显示ps命令的版本号</td>
</tr>
</tbody>
</table>
<p>部分GNU长参数可以和Unix或BSD风格的参数混用来定制输出。  </p>
<h5 id="xian-shi-jin-cheng-de-ceng-ji-xin-xi-forest"><a class="toclink" href="#xian-shi-jin-cheng-de-ceng-ji-xin-xi-forest">显示进程的层级信息--forest</a></h5>
<p>这么多参数中，--forest是一个很好的功能。它会显示进程的层级信息，并用ASCII字符绘出好看的效果：</p>
<div class="highlight"><pre><span></span><code>$ ps -ef --forest
UID        PID  PPID  C STIME TTY          TIME CMD
root         <span class="m">2</span>     <span class="m">0</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00 <span class="o">[</span>kthreadd<span class="o">]</span>
root         <span class="m">3</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00  <span class="se">\_</span> <span class="o">[</span>ksoftirqd/0<span class="o">]</span>

......

root      <span class="m">4256</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00 /usr/sbin/hypervkvpd -n
root      <span class="m">4258</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00 /usr/sbin/sshd -D
root      <span class="m">4770</span>  <span class="m">4258</span>  <span class="m">0</span> <span class="m">09</span>:28 ?        <span class="m">00</span>:00:00  <span class="se">\_</span> sshd: root@pts/0
root      <span class="m">4774</span>  <span class="m">4770</span>  <span class="m">0</span> <span class="m">09</span>:28 pts/0    <span class="m">00</span>:00:00      <span class="se">\_</span> -bash
root      <span class="m">5001</span>  <span class="m">4774</span>  <span class="m">0</span> <span class="m">11</span>:12 pts/0    <span class="m">00</span>:00:00          <span class="se">\_</span> ps -ef --forest
root      <span class="m">4259</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00 /usr/sbin/rsyslogd -n
root      <span class="m">4261</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:01 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      <span class="m">4503</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00 /usr/libexec/postfix/master -w
postfix   <span class="m">4517</span>  <span class="m">4503</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00  <span class="se">\_</span> qmgr -l -t unix -u
postfix   <span class="m">4958</span>  <span class="m">4503</span>  <span class="m">0</span> <span class="m">11</span>:00 ?        <span class="m">00</span>:00:00  <span class="se">\_</span> pickup -l -t unix -u
$ 
</code></pre></div>

<p>Unix风格的-H参数也有同样的功能，但是效果没有这个好：</p>
<div class="highlight"><pre><span></span><code>$ ps -efH
UID        PID  PPID  C STIME TTY          TIME CMD
root         <span class="m">2</span>     <span class="m">0</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00 <span class="o">[</span>kthreadd<span class="o">]</span>
root         <span class="m">3</span>     <span class="m">2</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00   <span class="o">[</span>ksoftirqd/0<span class="o">]</span>

......

root      <span class="m">4256</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00   /usr/sbin/hypervkvpd -n
root      <span class="m">4258</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00   /usr/sbin/sshd -D
root      <span class="m">4770</span>  <span class="m">4258</span>  <span class="m">0</span> <span class="m">09</span>:28 ?        <span class="m">00</span>:00:00     sshd: root@pts/0
root      <span class="m">4774</span>  <span class="m">4770</span>  <span class="m">0</span> <span class="m">09</span>:28 pts/0    <span class="m">00</span>:00:00       -bash
root      <span class="m">5002</span>  <span class="m">4774</span>  <span class="m">0</span> <span class="m">11</span>:13 pts/0    <span class="m">00</span>:00:00         ps -efH
root      <span class="m">4259</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00   /usr/sbin/rsyslogd -n
root      <span class="m">4261</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:01   /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      <span class="m">4503</span>     <span class="m">1</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00   /usr/libexec/postfix/master -w
postfix   <span class="m">4517</span>  <span class="m">4503</span>  <span class="m">0</span> <span class="m">09</span>:19 ?        <span class="m">00</span>:00:00     qmgr -l -t unix -u
postfix   <span class="m">4958</span>  <span class="m">4503</span>  <span class="m">0</span> <span class="m">11</span>:00 ?        <span class="m">00</span>:00:00     pickup -l -t unix -u
$ 
</code></pre></div>

<p>BSD风格的f参数也是这个功能，效果和--forest一样。<br>
<em>所以，任何情况下，都加上--forest就能获得最好的效果。</em>  </p>
<h3 id="shi-shi-jian-ce-jin-cheng-top"><a class="toclink" href="#shi-shi-jian-ce-jin-cheng-top">实时监测进程(top)</a></h3>
<p>top命令和ps命令相似，都能够显示进程信息，并且是实时的：</p>
<div class="highlight"><pre><span></span><code>$ top
top - <span class="m">12</span>:01:28 up  <span class="m">2</span>:41,  <span class="m">1</span> user,  load average: <span class="m">0</span>.00, <span class="m">0</span>.01, <span class="m">0</span>.05
Tasks:  <span class="m">97</span> total,   <span class="m">2</span> running,  <span class="m">95</span> sleeping,   <span class="m">0</span> stopped,   <span class="m">0</span> zombie
%Cpu<span class="o">(</span>s<span class="o">)</span>:  <span class="m">0</span>.0 us,  <span class="m">0</span>.3 sy,  <span class="m">0</span>.0 ni, <span class="m">99</span>.7 id,  <span class="m">0</span>.0 wa,  <span class="m">0</span>.0 hi,  <span class="m">0</span>.0 si,  <span class="m">0</span>.0 st
KiB Mem :  <span class="m">1816668</span> total,   <span class="m">335380</span> free,  <span class="m">1325796</span> used,   <span class="m">155492</span> buff/cache
KiB Swap:  <span class="m">2097148</span> total,  <span class="m">2097148</span> free,        <span class="m">0</span> used.   <span class="m">321636</span> avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                
 <span class="m">5028</span> root      <span class="m">20</span>   <span class="m">0</span>  <span class="m">161880</span>   <span class="m">2216</span>   <span class="m">1572</span> R  <span class="m">0</span>.3  <span class="m">0</span>.1   <span class="m">0</span>:00.06 top                                                                                    
    <span class="m">1</span> root      <span class="m">20</span>   <span class="m">0</span>  <span class="m">128000</span>   <span class="m">6544</span>   <span class="m">4136</span> S  <span class="m">0</span>.0  <span class="m">0</span>.4   <span class="m">0</span>:01.12 systemd                                                                                
    <span class="m">2</span> root      <span class="m">20</span>   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 kthreadd                                                                               
    <span class="m">3</span> root      <span class="m">20</span>   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.03 ksoftirqd/0                                                                            
    <span class="m">5</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 kworker/0:0H                                                                           
    <span class="m">6</span> root      <span class="m">20</span>   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.03 kworker/u2:0                                                                           
    <span class="m">7</span> root      rt   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 migration/0                                                                            
    <span class="m">8</span> root      <span class="m">20</span>   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 rcu_bh                                                                                 
    <span class="m">9</span> root      <span class="m">20</span>   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.35 rcu_sched                                                                              
   <span class="m">10</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 lru-add-drain                                                                          
   <span class="m">11</span> root      rt   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.11 watchdog/0                                                                             
   <span class="m">13</span> root      <span class="m">20</span>   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 kdevtmpfs                                                                              
   <span class="m">14</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 netns                                                                                  
   <span class="m">15</span> root      <span class="m">20</span>   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 khungtaskd                                                                             
   <span class="m">16</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 writeback                                                                              
   <span class="m">17</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 kintegrityd                                                                            
   <span class="m">18</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 bioset                                                                                 
   <span class="m">19</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 bioset                                                                                 
   <span class="m">20</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 bioset                                                                                 
   <span class="m">21</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 kblockd                                                                                
   <span class="m">22</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 md                                                                                     
   <span class="m">23</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 edac-poller                                                                            
   <span class="m">24</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 watchdogd                                                                              
   <span class="m">30</span> root      <span class="m">20</span>   <span class="m">0</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 kswapd0                                                                                
   <span class="m">31</span> root      <span class="m">25</span>   <span class="m">5</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 ksmd                                                                                   
   <span class="m">32</span> root      <span class="m">39</span>  <span class="m">19</span>       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.06 khugepaged                                                                             
   <span class="m">33</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 crypto                                                                                 
   <span class="m">41</span> root       <span class="m">0</span> -20       <span class="m">0</span>      <span class="m">0</span>      <span class="m">0</span> S  <span class="m">0</span>.0  <span class="m">0</span>.0   <span class="m">0</span>:00.00 kthrotld       
</code></pre></div>

<p>输出的第一部分显示的是系统的概况：</p>
<ul>
<li>第一行显示了当前时间、系统的运行时间、登录的用户数以及系统的平均负载。</li>
<li>平均负载有3个值：最近1分钟的、最近5分钟的、最近15分钟的平均负载</li>
<li>第二行显示了进程概要信息。这里将进程叫做任务（task），各种状态进程的数量：</li>
<li>total：总数</li>
<li>running：运行</li>
<li>sleeping：休眠</li>
<li>stopped：停止</li>
<li>zombie：僵化（僵化状态是指进程完成了，但父进程没有响应）</li>
<li>第三行显示了CPU的概要信息，将CPU利用率分成几类输出：</li>
<li>us【user space】— 用户空间占用CPU的百分比。</li>
<li>sy【sysctl】— 内核空间占用CPU的百分比。</li>
<li>ni【】— 改变过优先级的进程占用CPU的百分比</li>
<li>id【idolt】— 空闲CPU百分比</li>
<li>wa【wait】— IO等待占用CPU的百分比</li>
<li>hi【Hardware IRQ】— 硬中断占用CPU的百分比</li>
<li>si【Software Interrupts】— 软中断占用CPU的百分比</li>
<li>第四行是系统的物理内存状态：</li>
<li>total — 物理内存总量</li>
<li>free — 空闲内存总量</li>
<li>used — 使用中的内存总量</li>
<li>buff/cache — 用作内核缓存的内存量</li>
<li>第五行是系统的交换空间状态：</li>
<li>total — 交换区总量</li>
<li>free — 空闲交换区总量</li>
<li>used — 使用的交换区总量</li>
<li>avail Mem — 缓冲的交换区总量。内存中的内容被换出到交换区，而后又被换入到内存，但使用过的交换区尚未被覆盖，该数值即为这些内容已存在于内存中的交换区的大小，相应的内存再次被换出时可不必再对交换区写入。</li>
<li>最后一部分显示了当前运行中的进程的详细列表，有些列和ps命令的输出类似：</li>
<li>PID：进程的ID</li>
<li>USER：进程属主的名字</li>
<li>PR：进程的优先级</li>
<li>NI：进程的谦让度值（nice值）</li>
<li>VIRT：进程占用的虚拟内存总量</li>
<li>RES：进程占用的物理内存总量</li>
<li>SHR：进程和其他进程共享的内存总量</li>
<li>S：进程的状态（D可中断的休眠状态，R运行，S休眠，T跟踪状态或停止状态，Z僵化）</li>
<li>%CPU：进程使用的CPU时间比例</li>
<li>%MEM：进程使用的内存占可用内存的比例</li>
<li>TIME+：自进程启动到目前为止的CPU时间总量</li>
<li>COMMAND：进程所对应的命令行名称，也就是启动的程序名</li>
</ul>
<p>默认情况下，top启动后按%CPU排序，可按f指定其他列。轮询时间默认是3.0秒，可按d修改。按q是退出。其他就不列举了。  </p>
<h3 id="jie-shu-jin-cheng-kill"><a class="toclink" href="#jie-shu-jin-cheng-kill">结束进程(kill)</a></h3>
<p>在Linux中，进程之间通过<strong>信号</strong>来通信。大多数编写完善的程序都能接收和处理标准Unix进程信号。  </p>
<h4 id="linux-jin-cheng-xin-hao"><a class="toclink" href="#linux-jin-cheng-xin-hao">Linux进程信号</a></h4>
<p>下面的表格是一些常用的信号：</p>
<table>
<thead>
<tr>
<th align="left">信号</th>
<th align="left">名称</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">1</td>
<td align="left">HUP</td>
<td align="left">挂起</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left">INT</td>
<td align="left">中断</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left">QUIT</td>
<td align="left">结束运行</td>
</tr>
<tr>
<td align="left">9</td>
<td align="left">KILL</td>
<td align="left">无条件终止</td>
</tr>
<tr>
<td align="left">11</td>
<td align="left">SEGV</td>
<td align="left">段错误</td>
</tr>
<tr>
<td align="left">15</td>
<td align="left">TERM</td>
<td align="left">尽可能终止</td>
</tr>
<tr>
<td align="left">17</td>
<td align="left">STOP</td>
<td align="left">无条件停止运行，但不终止</td>
</tr>
<tr>
<td align="left">18</td>
<td align="left">TSTP</td>
<td align="left">停止或暂停，但继续在后台运行</td>
</tr>
<tr>
<td align="left">19</td>
<td align="left">CONT</td>
<td align="left">在STOP或TSTP之后恢复执行</td>
</tr>
</tbody>
</table>
<p>HUP信号有两个用途（两种情况）：<br>
通知程序与它的控制终端（Session）不再关联。登录系统时会分配给用户一个终端，在shell下执行的命令，就是和当前的终端关联的。用户终端连接(正常或非正常)结束时，就会用这个信号发出通知。而这个信号的默认操作就是终止进程。<br>
而对于与终端脱离关系的守护进程，这个信号用于通知它重新读取配置文件。<em>程序可以捕获这个信号，然后重载配置，从而避免重启来是最新配置生效。前提是程序有做这个优化。</em>  </p>
<p>上面的信号不全，可以用命令<code>kill -l</code>查看全部的信号。<br>
还可以用man命令查看信号的说明：<code>man 7 signal</code>。  </p>
<h4 id="fa-song-xin-hao-jie-shu-jin-cheng"><a class="toclink" href="#fa-song-xin-hao-jie-shu-jin-cheng">发送信号结束进程</a></h4>
<p>使用kill系列的命令，可以向进程发送信号。进程收到信号后，会执行相应的操作。未做特殊处理的程序，会执行信号的默认操作（一般就是终止进程）。<br>
程序上也可以捕获特定的信号，然后执行特点的操作（或设置为空操作，就是忽略该信号）。shell脚本中可以用trap命令来捕获信号并执行自定义的命令。这篇里有trap简单的说明： <a href="https://steeed.gitee.io/linux-command-tips.html#trap">https://steeed.gitee.io/linux-command-tips.html#trap</a>  </p>
<p><strong>kill命令</strong><br>
kill命令可通过进程ID（PID）给进程发信号。默认发送TERM信号。因为只能使用PID来指定，而不能用命令名，所以有时候不是很好用。<br>
要发送信号，你还必须是该进程的属主或者是root。<br>
kill命令不会有任何输出，要检查kill命令是由有效，可以再运行ps或top命令:</p>
<div class="highlight"><pre><span></span><code>$ ps -fp PID 
</code></pre></div>

<p><strong>killall命令</strong><br>
killall命令支持通过进程名而不是PID来结束进程。killall命令也支持通配符。<br>
最小安装是没有这个命令的，yum安装一下：</p>
<div class="highlight"><pre><span></span><code>yum install psmisc
</code></pre></div>
      </div>
      <div class="back-to-top">
        <a href="/">HOME</a>
        <a href="#top">TOP</a>
      </div>
    </article>
  </div>
<!-- end article -->
<!-- 页面往下滚动一段之后才会显示TOC -->
<script>
  window.onscroll = function() {
    var tocbox = document.getElementsByClassName('toc')[0];
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
    var osWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
    // console.log(osTop)
    if (osTop>300 && osWidth>865) {
      tocbox.style.display = "block"
    }
    if (osTop<300 || osWidth<865) {
      tocbox.style.display = "none"
    }
  }
</script>
                <footer>
                    <div class="icons">
                    </div>
                    <span id="busuanzi_container_page_pv" style="padding: 10px">本文阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                    <span id="busuanzi_container_site_pv" style="padding: 10px">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                    <span id="busuanzi_container_site_uv" style="padding: 10px">本站总访客数<span id="busuanzi_value_site_uv"></span>人</span>
                    <p>© <script>document.write(moment().format('YYYY'));</script> 749B</p>
                </footer>
        </div>
</body>
</html>